<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Forum/classes/PhpFox_ComponentTopicPost.class.php, $Author:  $ */



/** Post/edit topic.
 *
 * 1. oCurrUser (optional) current user object; if null or omitted - used current user
 *
 * @package module.forum
 * @version $Id: PhpFox_ComponentTopicPost.class.php 490 2008-09-18 11:41:52Z phpfoxsv $
 *
 */
class PhpFox_Mod_Forum_ComponentTopicPost extends BaseComponent
{

    /** Processing method
     */
	function process()
	{
        $oSrvSec = &App::getModuleService('Account', 'Security');
        /* @var $oSrvSec PhpFox_Mod_Account_ServiceSecurity */
        $oSrvSec->checkPerm('forum', 'full');

	    $oReq = &App::getRequest();
	    /* @var $oReq Request */

	    $oTxt = App::newObject('Text');

	    $oUser = $this->getParam('oUser', null);
	    if (!$oUser)
	    {
	        $oSrvSec = &App::getModuleService('Account', 'Security');
	        /* @var $oSrvSec PhpFox_Mod_Account_ServiceSecurity */
	        $oUser = $oSrvSec->getCurrentUser();
	    }
	 

	    $aValFields = array(
	        'title' => array('title'=> App::format('Mod_Forum.forum_subject'), 'def'=>'required'),
	        'text'  => array('title'=> App::format('Mod_Forum.forum_message'), 'def'=>'required'),
	    );

	    $oValidator = App::newObject('Validator', $aValFields);
	    /* @var $oValidator Validator */

	    $sTitle = '';
	    $sText = '';

        $oTopic = $this->_oModule->getItem('ForumTopic');
        /* @var $oTopic PhpFox_Mod_Forum_ItemForumTopic */


	    $iTopicId = $oReq->getInt('topic');


	    //got id -- this means update topic
	    if ($iTopicId)
	    {
            if ($oTopic->load($iTopicId))
            {
                if ( !$oSrvSec->isStaff() && ($oTopic->aData['user'] != $oUser->aData['user']) )
                {
                    App::gotoUrl('public.forum');
                }
        	    $sText = $oTxt->preParseText($oTxt->parseText($oTopic->aData['text'],0,0,1));
        	    $sTitle = $oTxt->preParseText($oTxt->parseText($oTxt->rmSpecialChars($oTopic->aData['title'])));

            }
            else
            {
                $iTopicId = 0;
            }
	    }

        switch($oReq->getAction())
        {
            //submit new topic
            case 'forum_post_topic':
                $sTitle = $oTxt->preParseText($oTxt->parseText($oTxt->rmSpecialChars($oReq->get('title'))));
        	    $sText  = $oTxt->preParseText($oTxt->parseText($oReq->get('text'),0,0,1));

        	    if (!trim($sTitle))
        	    {
        	        $this->addErrors(App::format('Mod_Forum.wrong.topic'));
        	    }
                if ($oUser)
                {
                    if ($oValidator->isValid($oReq->getAll()))
                    {
                   	if ( '' != trim(strip_tags($sText)) )
					{
                        $oSrvForum = &$this->_oModule->getService('Forum');
                        /* @var $oSrvForum PhpFox_Mod_Forum_ServiceForum */

                        $oForum = $this->_oModule->getItem('Forum');
                        if ($oForum->load($oReq->getInt('id')) and !$this->getErrors())
                        {
                            $iTopicId = $oSrvForum->addTopic($oUser, $oForum, $sTitle, $sText, $oReq->getInt('forum_type'));
                            $oSrvMessenger =&App::getModuleService('Messenger','Messenger');

                            if ($iTopicId)
                            {
                        
                            $oSrvAcc = &App::getModuleService('Account', 'Account');
                                $oSrvAcc->changeUserInfo($oUser, 'forum');
                                App::gotoUrl('public.forum.posts', array('id' => $iTopicId),
                                      App::format('Mod_Forum.topic_update'), 'message', 'ForumPosts');
                            }
                            else
                            {
                                $this->addErrors($oSrvForum->getErrors());
                            }
                        }
                        else
                        {
                            App::gotoUrl('public.forum');
                        }
                        
					}else{
                        	$this->addErrors(App::format('validator.rule.req',	array(App::format('Mod_Forum.forum_message'), App::format('Mod_Forum.forum_message')) ));
                    }
                    }
                    $this->addErrors($oValidator->getErrors());
                }
                else
                {
                    App::gotoUrl('public.forum');
                }
                break;

            case 'forum_update_topic':
                if (!$iTopicId)
                {
                    App::gotoUrl('public.forum');
                }
                if ($oTopic->load($iTopicId))
                {
                    $sTitle = $oReq->get('title');
            	    $sText  = $oReq->get('text');
            	    $iType = $oReq->getInt('forum_type');
            	    if ($oValidator->isValid($oReq->getAll()))
                    {
                    if ( '' != trim(strip_tags($sText)) )
					{
                        $oTopic->aData['title'] = $oTxt->preParseText($oTxt->parseText($oTxt->rmSpecialChars($sTitle)));
                        $oTopic->aData['text'] = $oTxt->preParseText($oTxt->parseText($sText,0,0,1));
                        $oTopic->aData['forum_type'] = $iType;
                        if (!trim($oTopic->aData['title']))
                	    {
                	        $this->addErrors(App::format('Mod_Forum.wrong.topic'));
                	    }
                        if ($oTopic->update(array('title', 'text', 'forum_type')) && !$this->getErrors())
                        {
                            App::gotoUrl('public.forum.posts', array('id' => $oTopic->aData['id']));
                        }
                        else
                        {
                            $this->addErrors($oTopic->getErrors());
                        }
                    }else{
                        	$this->addErrors(App::format('validator.rule.req',	array(App::format('Mod_Forum.forum_message'), App::format('Mod_Forum.forum_message')) ));
                    }
                    
                    }else
                    {
                        $this->addErrors($oValidator->getErrors());
                    }
                }
                break;

        }

        $aForumType = array(
            App::getParam('Mod_Forum.type.normal')       => App::format('Mod_Forum.normal'),
            App::getParam('Mod_Forum.type.announcement') => App::format('Mod_Forum.announcement'),
            App::getParam('Mod_Forum.type.sticky')       => App::format('Mod_Forum.sticky'),
        );

		$oTpl = &$this->_oModule->getTemplate();
		$oTpl->assignSrc(array(
            'aErrors'     => $this->getErrors(),
		    'jsValMess'   => $oValidator->makeJsMess(),
		    'jsValidator' => $oValidator->makeJS(),
		));
		$oTpl->assign(array(
		    'aTopic'     => $oTopic->aData,
		    'sTitle'     => $sTitle,
    	    'sText'      => $sText,
    	    'sAction'    => $iTopicId ? 'forum_update_topic' : 'forum_post_topic',
    	    'bShowType'  => $oSrvSec->isStaff(),
    	    'aForumType' => $aForumType,
	    ));

		$srcHtml = $oTpl->fetch('TopicPost.html');
		return $srcHtml;
	}

}
?>